$(function () {
    // setProductDetails();
    loadProductDetails();
});

let i = parseInt(sessionStorage.getItem("pdti"));//获取产品列表页传过来的产品json数据的索引值
let data = JSON.parse(sessionStorage.getItem("productData"));//获取产品列表页传过来的产品json数据
function loadProductDetails() {
    setProductType();
    setProductDetails();
}
function setProductType() {
    $(".pdt_type").html("");
    $.each(data.productTypes, function (j, producttype) {
        if ((j+1)==data.products[i].productType.id)
            $(".pdt_type").append("<div class='currentProductType' onclick='redirectProductOfType(" + producttype.id + ")'>" + producttype.name + "</div>");
        else
            $(".pdt_type").append("<div onclick='redirectProductOfType(" + producttype.id + ")'>" + producttype.name + "</div>");
    });
}
function redirectProductOfType(id) {
    sessionStorage.setItem("product_type",id);
    window.location.href = "/html/front/product.html";
}
function setProductDetails() {
    let last = i-1;//设置json数据上一个索引值
    let next = i+1;//设置json数据下一个索引值
    //根据产品id获取产品详细信息
    console.log(i);
    setData();
    //设置翻页
    pageJudge(last,next);
    $("#last_page").click(function () {
        if (last >= 0) {
            last--;
            i--;
            next--;
            setData();
            pageJudge(last,next);
        }else
            $("#last_page>span").html("无");
    });
    $("#next_page").click(function () {
        if (data.products[next] != undefined) {
            i++;
            last++;
            next++;
            setData();
            pageJudge(last,next);
        }
    });
}

function pageJudge(last,next) {
    if (last >= 0) //获取上一篇产品的产品名
        $("#last_page>span").html(data.products[last].name);
    else
        $("#last_page>span").html("无");

    if (data.products[next] != undefined) //获取下一篇产品的产品名
        $("#next_page>span").html(data.products[next].name);
    else
        $("#next_page>span").html("无");
}
function setData() {
    //将产品详细信息填充在产品详情列表
    $("#pdt_name").html(data.products[i].name);
    $("#pdt_name2").html(data.products[i].name);
    $("#pdt_intro").html(data.products[i].intro);
    $("#market_price>span").html(data.products[i].marketPrice);
    $("#price>span").html(data.products[i].price);
    $("#pdt_photo").attr("src", data.products[i].photo);
}